<?php
/*
	账户支付
	管理端支付接口
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('没有找到该活动，请重新选择！','error','jy_adbaoming/index/list');
	}
	$num=pdo_getcolumn('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'readnum');
	$num=$num+1;
	pdo_update('jy_adbaoming_list',array('readnum'=>$num),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$item=pdo_get('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$num1=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>1));
	$num2=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>2));
	$num3=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>4));
	$item['num']=$num1+$num2+$num3;
	$this->session('activity_id',$_RQ['id']);
	$item['hdp']=json_decode($item['hdp'],true);
	$item['remark']=json_decode($item['remark'],true);
	$item['diyfields']=json_decode($item['diyfrom'],true);
	$return['item']=$item;
	$return['xieyi']=$this->plugin_setting('xieyi');
	$endtime=strtotime($item['end']);
	if($endtime<time()){
		$return['isend']=1;
		$return['notitle']='活动已截至';
	}
	$total=pdo_count('jy_adbaoming_order',array('status'=>array(1,2,4),'uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id']));
	if($item['mannum']<=$total){
		$return['ismannum']=1;
		$return['notitle']='报名人数已满';
	}
	if($_SESSION['uid']){
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','mobile','avatar','nickname','realname'));
		$order=pdo_get('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid']),'','id desc');
		$name=$order['name']?:$member['realname'];
		$name=$name?:$member['nickname'];
		$name2=$name?cfc('tools')->substr($name,0,1).'**':'';
		$mobile=$order['mobile']?:$member['mobile'];
		$mobile2=$mobile?cfc('tools')->substr($mobile,0,7).'****':'';
	}
	$return['post']=array(
		'money'=>$item['money'],
		'oldname'=>$name,
		'oldname2'=>$name2,
		'oldmobile'=>$mobile,
		'oldmobile2'=>$mobile2,
		'diyfields'=>$item['diyfields'],
		'check'=>1,
	);
	$desc=strip_tags(str_replace('&nbsp;','',$item['content']));
	$return['sharedata']=array(
		'title'=>$item['title'],
		'desc'=>$desc?:$item['title'],
		'link'=>custom_url('jy_adbaoming/detail//id='.$item['id'],true),
		'imgUrl'=>ATTACHMENT_URL.$item['image'],
	);
	set_html('title',$item['title']);
	exi($return);
}
//支付
if($_RQ['op']=='notice'){
	$post=get_postdata();
	$xieyi=$this->plugin_setting('xieyi');
	if(!$post['check'] && $xieyi['noxieyi']!=1){
		exi('请先阅读《授权书》','error');
	}
	$activity=pdo_get('jy_adbaoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('activity_id')));
	$endtime=strtotime($activity['end']);
	if($endtime<time()){
		exi('活动已截至！不能再报名！','error','goback');
	}
	$total=pdo_count('jy_adbaoming_order',array('status'=>array(1,2,4),'uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id']));
	if($activity['mannum']<=$total){
		exi('报名人数已满！不能再报名！','error','goback');
	}
	//判断是否有数据
	if(!$post['name']){
		$post['name']=$post['oldname'];
	}
	if(empty($post['name'])){
		exi('请填写姓名','error');
	}
	if(!$post['mobile']){
		$post['mobile']=$post['oldmobile'];
	}
	if(empty($post['mobile'])){
		exi('请填写手机号码','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
		exi('请输入正确的手机号码','error');
	}
	foreach($post['diyfields'] as $item) {
		if($item['ismust']==1 && empty($item['data'])){
			exi('请完善'.$item['title'].'内容后再提交','error');
		}
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'activity_id'=>$this->session('activity_id'),
			'money'=>$post['money'],
			'pay'=>$post['pay'],
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'remark'=>trim($post['remark']),
			'diyfrom'=>ijson_encode($post['diyfields']),
			'ordersn'=>get_ordersn('jy_adbaoming_order','ordersn'),
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_adbaoming_order',$insert);
	$id=pdo_insertid();
	if($post['money']>0){
		$this->session('money',$post['money']);
		$params['fee']=$post['money'];
		$params['title']='活动报名订单';
		$params['id']=$id;
		$params['plugin']='jy_adbaoming';
		$params['back_url']='jy_adbaoming/book/payresult/';
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
		$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
		);
		exi($res);
	}else{
		pdo_update('jy_adbaoming_order',array('status'=>'2'),array('id'=>$id));
		exi('报名成功','success','jy_adbaoming/order/list/status=2&id='.$id);
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_adbaoming_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$order['price']=$this->session('money');
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_adbaoming_order',array('status'=>'2','pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_adbaoming/order/list/status=2&id='.$order['id']);
}